package com.gable.exhibition.util;

import com.gable.common.util.StringUtil;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by Lesson on 2018/2/28.
 */
public class InsideOutsideUtil {

    //获取当前设备是在保内还是保外
    public static  String queryMedicalSecurity(String guaranteeLastDate){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            if (StringUtil.isEmpty(guaranteeLastDate)) {
                return null;
            }

            Date date = sdf.parse(guaranteeLastDate);
            Date date1 = new Date();
            if(date.getTime() > date1.getTime()){
                return  "保内";
            }else {
                return "保外";
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    //获取设备在合同的保修截止日期
    public static String queryDeviceGuaranteedDate(String installDate,Integer deviceWarrantyPeriod){
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            if (installDate == null || deviceWarrantyPeriod == null) {
                return null;
            }
            Date date = sdf.parse(installDate);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.MONTH,deviceWarrantyPeriod);

            return sdf.format(calendar.getTime());

            /*Calendar calendar1 = Calendar.getInstance();

            Date date1=calendar.getTime();
            Date date2=calendar1.getTime();
            if(date1.getTime() > date2.getTime()){
                return  "保外";
            }else{
                return  "保内";
            }*/

        }catch (Exception e){
            e.printStackTrace();
        }

        return  null;
    }


    /**获取当前设备是在保内还是保外，是厂商保修还是供应商保修
     *1102	厂商保修
     *1103	保外
     *1105	供应商保修
     * @param enableTime 设备启用日期t_device.enable_time
     * @param deviceWarrantyPeriod 厂商保修期
     * @param supplierWarrantyPeriod 供应商保修期
     * @return
     */
    public static Long queryWarranty(String enableTime,Integer deviceWarrantyPeriod,Integer supplierWarrantyPeriod){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            if (StringUtil.isEmpty(enableTime) || (deviceWarrantyPeriod==null && supplierWarrantyPeriod==null)) {
                return 1103L;//保外
            }
            Calendar enableCalendar = Calendar.getInstance();
            Date date = sdf.parse(enableTime);
            enableCalendar.setTime(date);
            enableCalendar.add(Calendar.MONTH,deviceWarrantyPeriod==null?0:deviceWarrantyPeriod);
            Date now = sdf.parse(sdf.format(new Date()));
            //先判断厂商保修期
            if(enableCalendar.getTime().getTime() > now.getTime()){
                return  1102L;//厂商保修
            }
            //再判断供应商保修期
            //把启用时间重置
            enableCalendar.setTime(date);
            enableCalendar.add(Calendar.MONTH,supplierWarrantyPeriod==null?0:supplierWarrantyPeriod);
            if(enableCalendar.getTime().getTime() > now.getTime()){
                return 1105L;//供应商保修
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return 1103L;//保外
    }
}
